﻿<?php
class SubscriptionModel extends Model{

	public $id;
	public $afterwork_id;
	public $login_id;
	public $answer;
	public $vote;
	
	public function __construct()
	{
	}
	
	public function GetExisting($username, $afterwork_id)
	{		
		$db = new DbContext();
		$connec = $db->getConnection();
		
		$q = "SELECT s.* FROM subscription s INNER JOIN login l ON l.id = s.login_id WHERE s.afterwork_id = :af_id AND l.username = :username";
		
		$q=$connec->prepare($q);		
		$q->execute(array( 'af_id' => $afterwork_id, 'username' => $username));
		$q->setFetchMode(PDO::FETCH_CLASS|PDO::FETCH_PROPS_LATE, 'SubscriptionModel'); 
		
		return $q->fetch();
	}
	
	public function GetExistingAll($login_id)
	{		
		$db = new DbContext();
		$connec = $db->getConnection();
		
		$q = "SELECT s.* FROM subscription s INNER JOIN login l ON l.id = s.login_id WHERE l.id = :login_id";
		
		$q=$connec->prepare($q);		
		$q->execute(array( 'login_id' => $login_id));
		$q->setFetchMode(PDO::FETCH_CLASS|PDO::FETCH_PROPS_LATE, 'SubscriptionModel'); 
		
		return $q->fetchAll();
	}
	
	public function GetUsername()
	{
		return LoginModel::Get($this->login_id)->username;
	}
	
	public static function DeleteObsolete()
	{		
		$db = new DbContext();
		$connec = $db->getConnection();
		
		$q = "DELETE first FROM subscription AS first INNER JOIN afterwork AS second WHERE first.afterwork_id = second.id AND second.date < NOW();";
		
		$q=$connec->query($q);
	}
}
?>